<template>
	<div id="config">
		<section-item v-for="(item, index) in configs" :keys="item.config_keys" :value="item.value" :title="item.title" :type="item.type" :key="index"></section-item>
	</div>
</template>
<script>
import sectionItem from "./config-section";
import request from "winged/utils/axios";
import permission from "winged/utils/wt-permission";
import utils from "winged/utils/utils.js";
export default {
	components: {
		sectionItem
	},
	data() {
		return {
			PREFIX: [],
			configs: [], //[{keys:[{},{}],value:{}},{keys:[{},{}],value:{}}]
			all_services: [],
			templateConfig: {}
		};
	},
	methods: {
		getConfigKeys(type, callback) {
			//获取config keys type:0,1 0租户，1科室（不同科室configkeys相同）
			request(
				"uaa/config/get_config_keys",
				"post",
				{ data: JSON.stringify({ type: "" + type }) },
				true,
				response => {
					if (typeof response == "string") {
						response = JSON.parse(response);
					}
					response.config_keys = this.commonParse(
						response.config_keys
					);
					callback && callback(response.config_keys);
				},
				err => {
					console.log("err", err);
					this.$message("提交失败！");
				},
				false
			);
		},
		getConfigValue(doMain, callback) {
			//获取指定config的值
			request(
				"uaa/config/get_tenant_config",
				"post",
				{ data: JSON.stringify({ config_prefix: doMain }) },
				true,
				response => {
					if (typeof response == "string") {
						response = JSON.parse(response);
					}

					response = response.configs;
					// if(response.status && response.status == 'FAIL'){
					// 	this.$message(doMain + '配置获取失败')
					// 	console.warn(response.error_code)
					// 	response = {}
					// }
					// for(let i in response){
					// 	response[i] = utils.tryJsonParse(response[i],true)
					// }

					response.department = doMain; //在值中添加前缀字段
					//this.configs.push({value:response})
					// console.log('configs',response)
					//this.$set(this.configs, this.configs.length, {value:response})
					// this.$nextTick(() => {
					// 	this.configs = [].concat(this.configs)
					// 	console.log('configs',this.configs)
					// })
					//let config = this.configs[this.configs.length]
					// if(!config){
					// 	this.$set(this.configs, this.configs.length, {value:response})
					// 	//this.configs = this.configs.concat({value:response})
					// }else if(!config.value){
					// 	config.value = response
					// 	this.$set(this.configs, this.configs.length - 1, config)
					// }
					callback && callback(response);
				},
				err => {
					console.log("err-getPREFIX", err);
					this.$message("提交失败！");
				},
				false
			);
		},
		getData(domain, type) {
			this.getConfigKeys(type, config_keys => {
				this.templateConfig.config_keys = config_keys;
				this.getConfigValue(domain /*TENANT_PREFIX*/, value => {
					let pre = this.PREFIX.filter(item => {
						return item.domain == domain;
					})[0];
					if (pre) {
						this.templateConfig.title = pre.name;
					} else {
						this.templateConfig.title =
							value[domain + "#admin_display_name"];
					}
					this.templateConfig.value = value;
					this.templateConfig.type = type;
					this.$set(
						this.configs,
						this.configs.length,
						Object.assign({}, this.templateConfig)
					);
					let department_domain = utils.tryJsonParse(
						this.templateConfig.value[
							TENANT_PREFIX + "#department_domain"
						],
						true
					);
					if (department_domain) {
						if (
							department_domain.length == 1 &&
							domain == department_domain[0]
						) {
							return;
						}
						department_domain.forEach(item => {
							this.getData(item, type + 1);
						});
					}
				});
			});
		},
		get_micro_services(callback) {
			request(
				"uaa/config/get_micro_services",
				"post",
				{ data: "{}" },
				true,
				response => {
					if (typeof response == "string") {
						response = JSON.parse(response);
					}
					// console.log("all_services", response)
					this.all_services = response.micro_services;
					callback && callback();
					// console.log('all_services',this.all_services)
				},
				err => {
					// console.log('err-get_micro_services',err)
					this.$message("提交失败！");
				},
				false
			);
		},
		getPREFIX() {
			request(
				"uaa/user_info/get_department_list",
				"post",
				{
					data:
						'{"user_id":' +
						'"' +
						permission.getCurrentUserId() +
						'"' +
						',"parent_id":"0","page_index":"0","page_size":"999"}'
				},
				true,
				response => {
					if (typeof response == "string") {
						response = JSON.parse(response);
					}
					// console.log("getPREFIX", response)
					this.PREFIX = this.PREFIX.concat(response.departments);
				},
				err => {
					// console.log('err-getPREFIX',err)
					this.$message("提交失败！");
				},
				false
			);
		},
		commonParse: function(data) {
			data.forEach(item => {
				item.value = [];
				item.original_type = item.type;
				switch (item.name) {
					case "services":
						item.type = "checkbox";
						item.options = this.all_services;
						break;
					case "logo":
					case "logo_mini":
					case "admin_background":
					case "logo_large":
						item.type = "image";
						break;
					default:
						item.type = "text";
				}
			});
			return data;
		}
	},
	created() {
		this.getPREFIX();
		this.get_micro_services(() => {
			this.getData(TENANT_PREFIX, 0);
		});
		setTimeout(() => {
			// console.log(this.configs)
		}, 1000);
	}
};
</script>
<style lang="less">

</style>